package com.example.controller;

/**
 * Created by lenovo on 2018/5/20.
 */
import com.example.domain.FileUtil;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;

import java.io.InputStream;
import java.io.File;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;

@Controller
public class FileController {

    @Value("${web.project-data-path}")
    private  String filePath;

    @RequestMapping(value="/uploadFile", method = RequestMethod.POST)
    public @ResponseBody String uploadFile(@RequestParam("file") MultipartFile file,HttpServletRequest request) {
        String fileName = file.getOriginalFilename();
        try {
            FileUtil.uploadFile(file.getBytes(), filePath, fileName);
        } catch (Exception e) {
            return "error";
        }
        return "upload img success";
    }

    //根据projectid查找对应的cad文件名称
    @RequestMapping("/getCadNames")
    public @ResponseBody List<String> getCadNames(@RequestParam(value="projectId") String projectId) {
        return FileUtil.getCadNames(filePath,projectId);
    }

    //根据projectid和方案编号查找对应的所有图片名称
    @RequestMapping("/getImgNames")
    public @ResponseBody List<String> getImgNames(@RequestParam(value="projectId") String projectId,@RequestParam(value="schemaId") String schemaId) {
        return FileUtil.getImgNames(filePath,projectId,schemaId);
    }

    @RequestMapping(value="/getImgList", method = RequestMethod.GET)
    public @ResponseBody List<String> getFileList(HttpServletRequest request) {
      return  FileUtil.getFileList();
    }

    @RequestMapping(value="/downloadFile/{name}",method = RequestMethod.GET)
    public String downloadFile2(@PathVariable("name") String name, HttpServletResponse response) {
        String fileName = name;
        if (fileName != null) {
            String realPath = "D:/Files/";
            File file = new File(realPath , fileName);
            if (file.exists()) {
                response.setContentType("application/force-download");// 设置强制下载不打开
                response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                    System.out.println("success");
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        return null;
    }
}
